package cst.a802yp.myrecycleview_802yp.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;


import java.util.List;

import cst.a802yp.myrecycleview_802yp.R;
import cst.a802yp.myrecycleview_802yp.adapter.MyAdapter;
import cst.a802yp.myrecycleview_802yp.bean.ArticleBean;
import cst.a802yp.myrecycleview_802yp.iface.ArticleListener;
import cst.a802yp.myrecycleview_802yp.model.ArticleModel;

/**
 * Created by Administrator on 2017/5/23.
 */

public class ArticleFragment extends Fragment {
    private View view=null;
    private Context context;
    private RecyclerView recyclerView;
    private RecyclerView.LayoutManager layoutManager;
    private MyAdapter myAdapter;
    private List<ArticleBean> list=null;
    private static int cur_page=1;
    private static int lastVisibleItemPosition;
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.top01, container, false);
        context=view.getContext();
        init();
        ArticleModel articleModel =new ArticleModel();
        articleModel.getResultList("article",2,"ddrt4mi9rjnc0dm7sqgn50a837",articleListener);
        return view;
    }
    ArticleListener articleListener =new ArticleListener() {
        @Override
        public void onResponse(List<ArticleBean> listbean) {
          if(cur_page==1){
              list=listbean;
              myAdapter.setList(list);

          }else{
              list.removeAll(listbean);

              list.addAll(0,listbean);
              myAdapter.setList(list);
          }
        }

        @Override
        public void onFail(String msg) {
            Toast.makeText(context,"fail:"+msg,Toast.LENGTH_SHORT).show();
        }
    };
    private void init() {
        recyclerView = (RecyclerView) view.findViewById(R.id.rv_article);
        //创建默认的线性LayoutManager
        layoutManager = new LinearLayoutManager(context);
        //设置布局管理器
        recyclerView.setLayoutManager(layoutManager);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        recyclerView.setHasFixedSize(true);
        //创建并设置Adapter
        //        list=getData();
        myAdapter = new MyAdapter(context);
        myAdapter.setList(list);
        recyclerView.setAdapter(myAdapter);
        recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrollStateChanged(RecyclerView recyclerView, int newState) {

                super.onScrollStateChanged(recyclerView, newState);
                if (newState == RecyclerView.SCROLL_STATE_IDLE && lastVisibleItemPosition + 1 == list.size()) {
                    cur_page+=1;

                    new ArticleModel().getResultList("article",cur_page,"ddrt4mi9rjnc0dm7sqgn50a837",articleListener);
                }
            }

            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);
                lastVisibleItemPosition=list.size()-1;
            }
        });
    }
}
